using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WcfService1.DTO
{
    [DataContract]
    public class SalesOrderDTO : INotifyPropertyChanged
    {
        private String _Address;
        private Decimal _AfterDiscount;
        private CostCenterDTO _CostCenter;
        private CurrencyDTO _Currency;
        private String _CustomerName;
        private String _DeliveryAddress;
        private DeliveryTypeDTO _DeliveryType;
        private Decimal _Discount;
        private String _DiscountType;
        private bool _IsPpn;
        private String _Keterangan;
        private int _No;
        private Decimal _Ppn;
        private PreSalesOrderDTO _PreSalesOrder;
        private Decimal _Rate;
        private DateTime? _SODate;
        private String _SONo;
        private ObservableCollection<SalesOrderDetailDTO> _SalesOrderDetails;
        private EmployeeDTO _Salesman;
        private Status _Status;
        private Decimal _SubTotal;
        private String _Telp;
        private String _Term;
        private Decimal _Total;
        private String _Type;
        private WarehouseDTO _Warehouse;
        private String _created_by;
        private DateTime? _created_date;
        private String _modified_by;
        private DateTime? _modified_date;
        private string _PSNo;
        private string _SINo;
        private string _DONo;

        public SalesOrderDTO()
        {
            SODate = DateTime.Now.Date;
            
        }

        [DataMember]
        public String SONo
        {
            get { return _SONo; }
            set
            {
                if (SONo == value) return;
                _SONo = value;
                RaisePropertyChanged("SONo");
            }
        }


        [DataMember]
        public DateTime? SODate
        {
            get { return _SODate; }
            set
            {
                if (SODate == value) return;
                _SODate = value;
                RaisePropertyChanged("SODate");
            }
        }


        [DataMember]
        public DeliveryTypeDTO DeliveryType
        {
            get { return _DeliveryType; }
            set
            {
                _DeliveryType = value;
                RaisePropertyChanged("DeliveryType");
            }
        }


        [DataMember]
        public String DeliveryAddress
        {
            get { return _DeliveryAddress; }
            set
            {
                _DeliveryAddress = value;
                RaisePropertyChanged("DeliveryAddress");
            }
        }


        [DataMember]
        public CurrencyDTO Currency
        {
            get { return _Currency; }
            set
            {
                _Currency = value;
                RaisePropertyChanged("Currency");
            }
        }

        [DataMember]
        public EmployeeDTO Salesman
        {
            get { return _Salesman; }
            set
            {
                _Salesman = value;
                RaisePropertyChanged("Salesman");
            }
        }


        [DataMember]
        public Decimal Rate
        {
            get { return _Rate; }
            set
            {
                _Rate = value;
                RaisePropertyChanged("Rate");
            }
        }


        [DataMember]
        public String Term
        {
            get { return _Term; }
            set
            {
                _Term = value;
                RaisePropertyChanged("Term");
            }
        }


        [DataMember]
        public Decimal Ppn
        {
            get { return _Ppn; }
            set
            {
                _Ppn = value;
                RaisePropertyChanged("Ppn");
            }
        }


        [DataMember]
        public String Keterangan
        {
            get { return _Keterangan; }
            set
            {
                _Keterangan = value;
                RaisePropertyChanged("Keterangan");
            }
        }


        [DataMember]
        public String created_by
        {
            get { return _created_by; }
            set
            {
                _created_by = value;
                RaisePropertyChanged("created_by");
            }
        }


        [DataMember]
        public DateTime? created_date
        {
            get { return _created_date; }
            set
            {
                _created_date = value;
                RaisePropertyChanged("created_date");
            }
        }

        [DataMember]
        public String modified_by
        {
            get { return _modified_by; }
            set
            {
                _modified_by = value;
                RaisePropertyChanged("modified_by");
            }
        }


        [DataMember]
        public DateTime? modified_date
        {
            get { return _modified_date; }
            set
            {
                _modified_date = value;
                RaisePropertyChanged("modified_date");
            }
        }

        [DataMember]
        public ObservableCollection<SalesOrderDetailDTO> SalesOrderDetails
        {
            get { return _SalesOrderDetails; }
            set
            {
                if (value == SalesOrderDetails) return;
                _SalesOrderDetails = value;
                RaisePropertyChanged("SalesOrderDetails");
                RefreshNumber();
            }
        }


        [DataMember]
        public Decimal Discount
        {
            get { return _Discount; }
            set
            {
                _Discount = value;
                RaisePropertyChanged("Discount");
            }
        }


        [DataMember]
        public String DiscountType
        {
            get { return _DiscountType; }
            set
            {
                _DiscountType = value;
                RaisePropertyChanged("DiscountType");
            }
        }


        [DataMember]
        public PreSalesOrderDTO PreSalesOrder
    {
            get { return _PreSalesOrder; }
            set
            {
                if (ReferenceEquals(_PreSalesOrder,value) || value == null) return;
                 _PreSalesOrder = value;
                RaisePropertyChanged("PreSalesOrder");
            }
        }


        [DataMember]
        public String CustomerName
        {
            get { return _CustomerName; }
            set
            {
                _CustomerName = value;
                RaisePropertyChanged("CustomerName");
            }
        }


        [DataMember]
        public String Address
        {
            get { return _Address; }
            set
            {
                _Address = value;
                RaisePropertyChanged("Address");
            }
        }


        [DataMember]
        public String Telp
        {
            get { return _Telp; }
            set
            {
                _Telp = value;
                RaisePropertyChanged("Telp");
            }
        }


        [DataMember]
        public String Type
        {
            get { return _Type; }
            set
            {
                _Type = value;
                RaisePropertyChanged("Type");
            }
        }

        [DataMember]
        public String PSNo
        {
            get { return _PSNo; }
            set
            {
                _PSNo = value;
                RaisePropertyChanged("PSNo");
            }
        }

        [DataMember]
        public String SINo
        {
            get { return _SINo; }
            set
            {
                _SINo = value;
                RaisePropertyChanged("SINo");
            }
        }

        [DataMember]
        public String DONo
        {
            get { return _DONo; }
            set
            {
                _DONo = value;
                RaisePropertyChanged("DONo");
            }
        }

        public int No
        {
            get { return _No; }
            set
            {
                _No = value;
                RaisePropertyChanged("No");
            }
        }

        public Decimal Total
        {
            get { return _Total; }
            set
            {
                _Total = value;
                RaisePropertyChanged("Total");
            }
        }

        public bool IsPpn
        {
            get { return _IsPpn; }
            set
            {
                _IsPpn = value;
                RaisePropertyChanged("IsPpn");
            }
        }

        public Decimal SubTotal
        {
            get { return _SubTotal; }
            set
            {
                _SubTotal = value;
                RaisePropertyChanged("SubTotal");
            }
        }

        public Decimal AfterDiscount
        {
            get { return _AfterDiscount; }
            set
            {
                _AfterDiscount = value;
                RaisePropertyChanged("AfterDiscount");
            }
        }

        [DataMember]
        public CostCenterDTO CostCenter
        {
            get { return _CostCenter; }
            set
            {
                _CostCenter = value;
                RaisePropertyChanged("CostCenter");
            }
        }

        [DataMember]
        public WarehouseDTO Warehouse
        {
            get { return _Warehouse; }
            set
            {
                _Warehouse = value;
                RaisePropertyChanged("Warehouse");
            }
        }

        [DataMember]
        public Status Status
        {
            get { return _Status; }
            set
            {
                _Status = value;
                RaisePropertyChanged("Status");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void RefreshNumber()
        {
            SalesOrderDetails.CollectionChanged += SalesOrderDetails_CollectionChanged;
            int number = 0;
            foreach (SalesOrderDetailDTO DetailDto in SalesOrderDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        private void SalesOrderDetails_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            int number = 0;
            foreach (SalesOrderDetailDTO DetailDto in SalesOrderDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        public void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}